package com.example.yls.cnbetaapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.yls.cnbetaapp.R;
import com.example.yls.cnbetaapp.component.OnTvGlogbalLayoutListener;
import com.example.yls.cnbetaapp.entity.MessageEntity;
import com.example.yls.cnbetaapp.httpService.MessageService;

import java.util.List;

public class MessageAdapter extends BaseAdapter {
    private List<MessageEntity> messageEntities;
    private LayoutInflater inflater = null;
    private Context context;
    public MessageAdapter(List<MessageEntity> messageEntities, Context context){
        this.messageEntities =messageEntities;
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return messageEntities.size();
    }

    @Override
    public Object getItem(int i) {
        return getItemId(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = inflater.inflate(R.layout.list_item, null);
        ImageView img_icon = (ImageView) view.findViewById(R.id.image);
        TextView txt_type = (TextView)view.findViewById(R.id.type);
        TextView txt_statistical = (TextView)view.findViewById(R.id.statistical);
        TextView txt_aName = (TextView) view.findViewById(R.id.title);
        TextView txt_aSpeak = (TextView) view.findViewById(R.id.intro);
//        TextView txt_titleUrl = (TextView) view.findViewById(R.id.titleUrl);
//        img_icon.setBackgroundResource(messageEntities.get(position).getaIcon());
        TextView txt_mid  = (TextView) view.findViewById(R.id.mid);
        txt_aName.setText(messageEntities.get(position).getTitle());
        txt_aSpeak.setText(messageEntities.get(position).getIntro());
        txt_aSpeak.getViewTreeObserver().addOnGlobalLayoutListener(new OnTvGlogbalLayoutListener(txt_aSpeak));
        String url = messageEntities.get(position).getImageUrl();
        txt_type.setText("| " +messageEntities.get(position).getType());
        txt_statistical.setText("发布于"+messageEntities.get(position).getIssureTime()+" | "+messageEntities.get(position).getReadingTimes()+"次阅读 | "+messageEntities.get(position).getCommentCount()+"个意见");
        txt_mid.setText(String.valueOf(messageEntities.get(position).getMid()));
        MessageService getImageByUrlUtil = new MessageService();
        getImageByUrlUtil.setImage(img_icon,url);
        return view;
    }
}
